package org.jctc.els.modules.biz.service.elsTeamMembers.impl;

import cn.hutool.core.util.BooleanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import org.jctc.els.modules.biz.entity.elsTeamMembers.ElsTeamMembers;
import org.jctc.els.modules.biz.mapper.elsTeamMembers.ElsTeamMembersMapper;
import org.jctc.els.modules.biz.mapper.elsUserProject.ElsUserProjectMapper;
import org.jctc.els.modules.biz.service.elsTeamMembers.ElsTeamMembersService;
import org.jctc.els.modules.biz.vo.ElsTeamMembersVO;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author maxinyu
 * @title: ElsTeamMembersService
 * @projectName evaluationsystem-boot
 * @description:
 * @date 2022/6/2319:08
 */
@Service
@AllArgsConstructor
public class ElsTeamMembersServiceImpl extends BaseServiceImpl<ElsTeamMembersMapper, ElsTeamMembers>  implements ElsTeamMembersService {
    @Autowired
    private ElsTeamMembersMapper teamMembersMapper;

    @Autowired
    private ElsUserProjectMapper userProjectMapper;

    @Override
    public int selectUserAlreadyExists(Long userId, Long teamId) {
        return teamMembersMapper.selectUserAlreadyExists(userId,teamId);
    }

    @Override
    public IPage<ElsTeamMembersVO> getExpertsList(IPage<ElsTeamMembersVO> page, String teamId) {
        return page.setRecords(teamMembersMapper.getExpertsList(page,teamId));
    }

    @Override
    public void removeList(List<Long> ids) {
        for (int i = 0; i < ids.size(); i++) {
            teamMembersMapper.removeList(ids.get(i));
        }
    }

    @Override
    public boolean updateLeader(String id,Integer canLeader) {
        return teamMembersMapper.updateLeader(id,canLeader);
    }

    @Override
    public ElsTeamMembers selectUserLeader(String teamId) {
        return teamMembersMapper.selectUserLeader(teamId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeTeamMembers(Long teamId, List<Long> userIds) {
        //删除小组中负责的项目
        userProjectMapper.removeTeamMembersProject(teamId, userIds);
        //删除小组中的专家
        boolean result = teamMembersMapper.removeTeamMembers(teamId, userIds) > 0;
        if(BooleanUtil.isFalse(result)){
            throw new ServiceException("操作失败");
        }
        return result;
    }
}
